import Axios from "axios/lib/axios";
// const basePath = 'http://cloud-services.e-lingcloud.com'
const basePath = "http://localhost:8080/xb";

// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
  // 在发送请求之前做某事
  return config;
}, function (error) {
  // .请求错误时做些事
  return Promise.reject(error);
});

// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
  // 对响应数据做些事
  return response;
}, function (error) {
  // 请求错误时做些事
  return Promise.reject(error);
});

export function getBasePath () {
  return basePath;
}

export function post (url, data) {
  return postJSON(url, data, false);
}

export function postJSON (url, data, requestJson) {
  if (requestJson) {
    return Axios.post(basePath + url, JSON.stringify(data), {
      timeout: 30000,
      headers: {
        "Content-Type": "application/json;encoding=UTF-8"
      }
    });
  } else {
    return Axios.post(basePath + url, data, {
      timeout: 30000,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
      },
      transformRequest: [function (data) {
        let ret = "";
        for (let it in data) {
          ret += encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        let token = localStorage.getItem("token");
        if (token) {
          ret += "tk=" + token + "&";
        }
        return ret.substring(0, ret.length);
      }]
    }).catch(function (error) {
      let httpCode = error.response.status;
      if (httpCode === 403) {
        alert("登录超时,请重新登录");
      } else {
        alert("网络错误");
      }
    });
  }
}

export function formatDate (date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}

function padLeftZero (str) {
  return ("00" + str).substr(str.length);
}

export function adminRoles () {
  return [
    {"id": "13080110395587872009", "name": "admin", "text": "管理员"},
    {"id": "16042009550419534631", "name": "omni", "text": "客服人员"},
    {"id": "16042009553639948082", "name": "roadMgr", "text": "路线管理"},
    {"id": "17080114192761827813", "name": "activityMgr", "text": "活动管理"}
  ];
}

export function orderStatus () {
  return [
    {"value": 1, "label": "待确认"},
    {"value": 2, "label": "待支付"},
    {"value": 3, "label": "支付中"},
    {"value": 4, "label": "支付成功"},
    {"value": 5, "label": "支付失败"}
  ];
}

export function formRules (propNames) {
  let rules = {
    username: [
      {required: true, message: "请输入名称", trigger: "blur"}
    ],
    usercode: [
      {required: true, message: "请输入标识", trigger: "blur"}
    ],
    mobile: [
      {required: true, message: "手机号不能为空", trigger: "blur"},
      {pattern: /^[1][3,4,5,7,8][0-9]{9}$/, message: "手机号格式不对"}
    ],
    password: [
      {required: true, message: "请输入密码", trigger: "blur"}
    ],
    role: [
      {required: true, message: "请选择角色", trigger: "blur"}
    ],
    code: [
      {required: true, message: "手机验证码不能为空"}
    ],
    imgCode: [
      {required: true, message: "图形验证码不能为空"}
    ]
  };
  let newRules = {};
  for (let i = 0; i < propNames.length; i++) {
    newRules[propNames[i]] = rules[propNames[i]];
  }
  return newRules;
}
